# Maintainer: David Runge <dvzrv@archlinux.org>

pkgbase=cardinal
pkgname=(cardinal cardinal-clap cardinal-data cardinal-jack cardinal-lv2 cardinal-vst cardinal-vst3)
pkgver=23.02
pkgrel=2
pkgdesc="Virtual modular synthesizer"
arch=(loong64 x86_64)
url="https://github.com/DISTRHO/Cardinal"
license=(GPL3)
makedepends=(
  cmake
  dbus
  file
  gcc-libs
  gendesk
  git
  glibc
  jansson
  libarchive
  libglvnd
  liblo
  libsndfile
  libsamplerate
  libx11
  libxext
  libxrandr
  mold
  python
  speexdsp
)
checkdepends=(
  kxstudio-lv2-extensions
  lv2
  lv2lint
  mod-lv2-extensions
  xorg-server-xvfb
)
source=(
  https://github.com/DISTRHO/$pkgbase/releases/download/$pkgver/$pkgbase-$pkgver.tar.xz
  $pkgbase-$pkgver.svg::https://raw.githubusercontent.com/DISTRHO/Cardinal/eb95b5990cf734c10f2caab1a246eac893f0266d/plugins/Cardinal/orig/distrho.svg
)
sha512sums=('60d6dcb2f3c90e41739c645f75408dc1603c5c999819577be26dbc7d05e2fbbce8dbf3d8d20d7d9e3075459f0fadb6f32c17d98c2186cf0ebed767f016410aec'
            '31a7d1e548285af0ead1bc844fbb1d35b50f6284159047cb401a829d4068992fa328770e42c377c08d013f6787e55ac12b94eba4d5af4b90373b157971fc8835')
b2sums=('c28b1aff34c3e1b5fff01cf74c2df38e4ed58e7c56ccb5e106ef8582675e2914e4973bca874053371521150a838007d4eaa0e8ea58899337c22a2f5b22dfd5cd'
        'd82fefe15234c1ed3a4d487c6082a2e3ac73b60f11041314e99c5a5de5b3ea141efbd5afd18851510bd4f801fd71f7cd89d54d86258d1ba750bbfd762aa37a8d')

_common_depends=(
  cardinal-data
  dbus libdbus-1.so
  file libmagic.so
  gcc-libs
  glibc
  jansson
  libarchive libarchive.so
  libglvnd libGL.so
  liblo liblo.so
  libsamplerate libsamplerate.so
  libsndfile libsndfile.so
  libx11
  libxext
  libxrandr
  speexdsp libspeexdsp.so
)
_common_optdepends=(
  'carla: for carla module and ildaeil plugin host'
  'mpv: for experimental video support'
  'xdg-desktop-portal-impl: for file browser'
)

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  gendesk -n \
          --exec Cardinal \
          --name Cardinal \
          --pkgname studio.kx.distrho.Cardinal \
          --pkgdesc "$pkgdesc JACK standalone" \
          --icon $pkgbase \
          --genericname "Virtual modular synthesizer"
}

build() {
  export CFLAGS+=" -B/usr/lib/mold -Wno-format-security"
  export CXXFLAGS+=" -B/usr/lib/mold -Wno-format-security"
  make PREFIX=/usr SYSDEPS=true WITH_LTO=true -C $pkgname-$pkgver
}

check() {
  declare -A _links=(
    ["Cardinal"]="https://distrho.kx.studio/plugins/cardinal"
    ["CardinalFX"]="https://distrho.kx.studio/plugins/cardinal#fx"
    ["CardinalSynth"]="https://distrho.kx.studio/plugins/cardinal#synth"
  )

  # NOTE: lvlint fails on Cardinal, as it makes use of non-standard Port Class, which it does not support yet
  for _name in Cardinal{FX,Synth}; do
    xvfb-run lv2lint -s "lv2_generate_ttl" -Mpack -I $pkgname-$pkgver/bin/$_name.lv2 "${_links[$_name]}"
  done
}

package_cardinal() {
  depends=(cardinal-jack cardinal-lv2 cardinal-vst cardinal-vst3)

  make PREFIX=/usr SYSDEPS=true DESTDIR="$pkgdir" install -C $pkgbase-$pkgver

  (
    cd "$pkgdir"
    # have find-libdeps resolve dependencies properly:
    # https://gitlab.archlinux.org/archlinux/devtools/-/issues/102
    find usr/lib -type f \( -iname "*.clap" -or -iname "*.so" \) -exec chmod +x {} \;

    _pick $pkgbase-clap usr/lib/clap/
    _pick $pkgbase-data usr/share/$pkgbase
    _pick $pkgbase-data usr/share/doc
    _pick $pkgbase-jack usr/bin/
    _pick $pkgbase-lv2 usr/lib/lv2/
    _pick $pkgbase-vst usr/lib/vst/
    _pick $pkgbase-vst3 usr/lib/vst3/
  )
}

package_cardinal-clap() {
  pkgdesc+=" - CLAP plugin"
  groups=(clap-plugins pro-audio)
  depends=("${_common_depends[@]}" clap-host)
  optdepends=("${_common_optdepends[@]}")

  mv -v $pkgbase-clap/* "$pkgdir"
}

package_cardinal-data() {
  pkgdesc+=" - data"

  mv -v $pkgbase-data/* "$pkgdir"
}

package_cardinal-jack() {
  pkgdesc+=" - JACK standalone"
  groups=(pro-audio)
  depends=("${_common_depends[@]}" hicolor-icon-theme jack)
  optdepends=("${_common_optdepends[@]}")

  mv -v $pkgbase-jack/* "$pkgdir"
  install -vDm 644 *.desktop -t "$pkgdir/usr/share/applications"
  install -vDm 644 $pkgbase-$pkgver.svg "$pkgdir/usr/share/icons/hicolor/scalable/apps/$pkgbase.svg"
}

package_cardinal-lv2() {
  pkgdesc+=" - LV2 plugin"
  groups=(lv2-plugins pro-audio)
  depends=("${_common_depends[@]}" lv2-host)
  optdepends=("${_common_optdepends[@]}")

  mv -v $pkgbase-lv2/* "$pkgdir"
}

package_cardinal-vst() {
  pkgdesc+=" - VST2 plugin"
  groups=(pro-audio vst-plugins)
  depends=("${_common_depends[@]}" vst-host)
  optdepends=("${_common_optdepends[@]}")

  mv -v $pkgbase-vst/* "$pkgdir"
}

package_cardinal-vst3() {
  pkgdesc+=" - VST3 plugin"
  groups=(pro-audio vst3-plugins)
  depends=("${_common_depends[@]}" vst3-host)
  optdepends=("${_common_optdepends[@]}")

  mv -v $pkgbase-vst3/* "$pkgdir"
}
